package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userservice = new UserServiceImpl();
    private  static final String FALG_REG="reg";//注册页面
    private  static final String FALG_REG_SUBMIT="rs";//提交注册

    private  static final String FALG_LOGIN="login";//登陆页面
    private  static final String FALG_LOGIN_SUBMIT="ls";//提交登录

    private  static final String FALG_INFO="info";//用户中心页面
    private  static final String FALG_LOGOUT="logout";//注销
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String flag= request.getParameter("flag");
        System.out.println("flag="+flag);
        if(StringUtils.isBlank(flag)){//跳转到注册页面
            //toLoginPage(request,response);
        }else if(FALG_REG.equals(flag)){//跳转到注册页面
            toRegisterPage(request,response);
        }else if(FALG_REG_SUBMIT.equals(flag)){//执行注册
            register(request,response);
        }else if(FALG_INFO.equals(flag)){//跳转到用户中心
            toUserInfo(request,response);
        }else if(FALG_LOGOUT.equals(flag)){//用户注销
            logout(request,response);
        }else if(FALG_LOGIN.equals(flag)){//跳转到登陆页面
            toLoginPage(request,response);
        }else if(FALG_LOGIN_SUBMIT.equals(flag)){//执行登录
            login(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    /**
     * 注册操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void register(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username= request.getParameter("username");
        String mobilephone= request.getParameter("phone");
        String password= request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("checkcode");
        if(checkcode.equalsIgnoreCase(vicode)){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setMobilePhone(mobilephone);
            String userId=userservice.saveUser(user);
            User user2=userservice.findUserById(userId);
            session.setAttribute("user",user2);
            request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            return;
        }else {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
        }
    }
    private void toRegisterPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
    }
    private void toLoginPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }
    private void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        User user=userservice.findUserByNamePw(username,password);
        if(user==null){
            request.setAttribute("message","用户名或密码错误");
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    }
    private void logout(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        HttpSession session = request.getSession();
        session.invalidate();
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
    private void toUserInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
    }

}
